//
//  CJProductDetailController.m
//  Boss
//
//  Created by admin on 2018/9/17.
//  Copyright © 2018年 admin. All rights reserved.
//

#import "CJProductDetailController.h"
#import "CJProductDetailImageCell.h"
#import "CJProductDetailHeaderCell.h"
#import "CJAddFirmProductController.h"
@interface CJProductDetailController ()<UITableViewDelegate,UITableViewDataSource,CJProductDetailImageCellDelegate>
@property (nonatomic,strong)UITableView *tableView;
@end

@implementation CJProductDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}
-(void)setupUI
{
    [self.view addSubview:self.tableView];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"CJProductDetailImageCell" bundle:nil] forCellReuseIdentifier:@"CJProductDetailImageCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"CJProductDetailHeaderCell" bundle:nil] forCellReuseIdentifier:@"CJProductDetailHeaderCell"];
    self.tableView.estimatedRowHeight = 200;
    
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"return_black_icon"] style:(UIBarButtonItemStyleDone) target:self action:@selector(returnToTop)];
    self.navigationItem.rightBarButtonItem  = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"more_black_icon"] style:(UIBarButtonItemStyleDone) target:self action:@selector(moreAction)];
//    [self wr_setNavBarTintColor:[UIColor colorWithWhite:0.0 alpha:0.8]];
     [self wr_setNavBarBackgroundAlpha:0];
}
-(void)returnToTop
{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)moreAction
{
    WEAKSELF
    UIAlertController * alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:(UIAlertControllerStyleActionSheet)];
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    }];
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"编辑" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [weakSelf editAction];
    }];
    UIAlertAction *action3 = [UIAlertAction actionWithTitle:@"删除" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [weakSelf deleteAction];
        
    }];
    [alertController addAction:action1];
    [alertController addAction:action2];
    [alertController addAction:action3];
    if (IS_IPAD) {
        //        UIPopoverPresentationController *popPresenter = [alertController popoverPresentationController];
        //        popPresenter.sourceView = cell;
        //        popPresenter.sourceRect = cell.bounds;
        //        [self presentViewController:alertController animated:YES completion:nil];
    }else{
        [self presentViewController:alertController animated:YES completion:nil];
    }
    
    
    
    
}

-(void)deleteAction
{
    [WYHttpServer deleteFirmProductWithID:self.model.id SucessBlock:^(BOOL success, id response, NSString *errorMessage) {
        if (success) {
            [self.navigationController popViewControllerAnimated:YES];
        }
        
        
        
        
        
    }];
}

-(void)editAction
{
    CJAddFirmProductController *afpc = [[CJAddFirmProductController alloc]init];
    
    afpc.model = self.model;
    
    
    [self.navigationController pushViewController:afpc animated:YES];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row==0) {
        CJProductDetailHeaderCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CJProductDetailHeaderCell" forIndexPath:indexPath];
        if (!cell) {
            cell = [[NSBundle mainBundle]loadNibNamed:@"CJProductDetailHeaderCell" owner:nil options:nil].firstObject;
        }
        [cell.productImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@!default",self.model.productCover]] placeholderImage:nil];
        cell.productNameLabel.text = self.model.productName;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
        
        
    }
    if (indexPath.row==1) {
        CJProductDetailImageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CJProductDetailImageCell" forIndexPath:indexPath];
        if (!cell) {
            cell = [[NSBundle mainBundle]loadNibNamed:@"CJProductDetailImageCell" owner:nil options:nil].firstObject;
        }
        cell.productName.text = self.model.productDetails;
        cell.dataArray = [self.model.url componentsSeparatedByString:@","].mutableCopy;
        cell.delegate =self;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    
    return nil;
}

-(void)didChangeHeight:(CJProductDetailImageCell *)productDetailCell
{
    [self.tableView reloadData];
}






- (UITableView *)tableView
{
    if (_tableView == nil) {
        CGRect frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
        _tableView = [[UITableView alloc] initWithFrame:frame
                                                  style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView setSeparatorStyle:(UITableViewCellSeparatorStyleNone)];
    }
    return _tableView;
}


@end
